package com.duanjw.mybatis.spring.config;

import com.duanjw.mybatis.spring.mapper.UserMapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyFactoryBean implements FactoryBean, InvocationHandler {
    public Class clazz;
    public MyFactoryBean(Class clazz) {
        this.clazz = clazz;
    }
    @Override
    public Object getObject() throws Exception {
        Class[] classes = {clazz};
        Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), classes, this);
        return proxy;
    }

    @Override
    public Class<?> getObjectType() {
        return UserMapper.class;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Method m = proxy.getClass().getInterfaces()[0].getMethod(method.getName());
        Select select = m.getDeclaredAnnotation(Select.class);
        System.out.println("需要执行的sql；" + select.value()[0]);
        return null;
    }
}
